package com.hspedu.exit;

/**
 * @author LiuJinTao
 * @className ThreadExit
 * @date 2024/06/23 16:15
 * @description TODO
 */
public class ThreadExit {
    public static void main(String[] args) throws InterruptedException {
        T t = new T();
        t.start();

        // 希望 main 线程去控制 t 线程的终止 修改 loop
        // 让 t 退出 run 方法，从而终止 t 线程，通知方法
        // 让 main 线程休眠 10 秒再退出
        Thread.sleep(10000);
        t.setLoop(false);
    }
}


class T extends Thread {
    private int count = 0;
    // 设置一个控制变量
    private boolean loop = true;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public boolean isLoop() {
        return loop;
    }

    public void setLoop(boolean loop) {
        this.loop = loop;
    }

    @Override
    public void run() {
        while (loop) {
            try {
                sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("T 运行中" + (++count));
        }
    }
}